package com.web4code.codegen.generator;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import com.web4code.codegen.config.GenConfig;
import com.web4code.codegen.constant.Constants;
import com.web4code.codegen.domain.GenProject;
import com.web4code.codegen.importer.FileImporter;
import com.web4code.codegen.utils.StringUtils;

/**
 * 代码生成主逻辑-工程代码生成
 * 
 * @author web4code
 */
public class GenerateProjectUtil {

    /**
     * 生成代码
     * @throws IOException
     */
    public static boolean generateProject() throws IOException
    {
        final GenConfig genConfig = GenConfig.getGenConfig();
        final String fullFilePath = genConfig.getDataSourceParam() + File.separator + "project.yaml";
        GenProject genProject = FileImporter.importFile(fullFilePath, GenProject.class);
        //
        generateProject(genProject);
        return true;
    }

    /**
     * 生成代码
     * @throws IOException
     */
    public static boolean generateProject(GenProject genProject) throws IOException
    {
        VelocityInitializer.initVelocity();

        VelocityContext context = VelocityProjectUtils.prepareContext(genProject);

        // 获取模板列表
        List<String> templates = VelocityProjectUtils.getTemplateList();
        for (String template : templates)
        {
            // 渲染模板
            StringWriter sw = new StringWriter();
            final String tplPath = getTplPath(genProject, template);
            Template tpl = Velocity.getTemplate(tplPath, Constants.UTF8);
            tpl.merge(context, sw);
            try
            {
                final String path = getGenPath(genProject, template);
                FileUtils.writeStringToFile(new File(path), sw.toString(), Constants.UTF8);
            }
            catch (IOException e)
            {
                throw new IOException("渲染模板失败，项目名：" + genProject.getProjectName());
            }
        }
        return true;
    }

    /**
     * 获取代码模板地址
     * 
     * @param genProject 项目信息
     * @param template 模板文件路径
     * @return 模板地址
     */
    private static String getTplPath(GenProject genProject, String template)
    {
        final String tplPath = genProject.getTemplatePath();
        if (StringUtils.isEmpty(tplPath)) {
            GenConfig genConfig = GenConfig.getGenConfig();
            return genConfig.getTemplatePath() + File.separator + template;
        }
        return genProject.getTemplatePath() + File.separator + template;
    }

    /**
     * 获取代码生成地址
     * 
     * @param genProject 项目信息
     * @param template 模板文件路径
     * @return 生成地址
     */
    private static String getGenPath(GenProject genProject, String template)
    {
        String genPath = genProject.getGenPath();
        if (StringUtils.isEmpty(genPath)) {
            GenConfig genConfig = GenConfig.getGenConfig();
            return genConfig.getGeneratePath() + File.separator + VelocityProjectUtils.getFileName(template, genProject);
        }
        if (StringUtils.equals(genPath, "/"))
        {
            return System.getProperty("user.dir") + File.separator + "src" + File.separator + VelocityProjectUtils.getFileName(template, genProject);
        }
        return genPath + File.separator + VelocityProjectUtils.getFileName(template, genProject);
    }

}
